function ScnPrefsAssistant(argFromPusher) {
}

ScnPrefsAssistant.prototype = {
	setup: function() {
		var PrefsCookie = new Mojo.Model.Cookie('PrefsCookie');
		var PrefsData = PrefsCookie.get();
		if (PrefsData !== undefined) {
			this.txtLatModel = {value : PrefsData.myLat};
			this.txtLonModel = {value : PrefsData.myLon};
			this.txtMaxDistModel = {value : PrefsData.myMaxDist};
			this.sliMaxDistModel = {value : PrefsData.myMaxDist};
			this.sliMagModel = {value : PrefsData.myMag};
			this.lblMagNbModel = {label : PrefsData.myMag + "+"};
			this.sliRefreshModel = {value : PrefsData.myPageRefresh};
			this.lblRefreshNbModel = {label : PrefsData.myPageRefresh + " min."};
			this.liseDistModel = {value : PrefsData.myDist};
//			this.liseInfoModel = {value : PrefsData.myInfo};
			this.cbTecPlatesModel = {value : PrefsData.myTecPlates};
			this.cbPalmDarkModel = {value : PrefsData.myPalmDark};
			this.cbOverrideModel = {value : PrefsData.myOverride};
			this.cbEMSCModel = {value :  PrefsData.myEMSC};
			this.cbUSGSModel = {value :  PrefsData.myUSGS};
		}
		this.lblMagModel = {label : $L("Minimum Magnitude")};
		this.lblMaxDistModel = {label : $L("Maximum distance from here")};
		this.lblRefreshModel = {label : $L("Refresh data every")};
		this.lblTecPlatesModel = {label : $L("Display the tectonic plates on map")};
		this.lblPalmDarkModel = {label : $L("Use dark color style")};
		this.lblMyLoc = {label : $L({value : "If GPS is switched off or not able to fix my position, use the following coordinates to calculate distance to the epicenter:", key : "lblMyLoc"})};
		this.lblLatModel = {label : $L("Latitude")};
		this.lblLonModel = {label : $L("Longitude")};
		this.btnGetMyLocModel = {label : $L("Get it from GPS")};
		this.lblOverrideModel = {label : $L("Overwrite those values each time a location fix is performed successfully")};
		this.lblEMSCdescModel = {label : $L({value : "Focus on Europe, Asia and Pacific region", key : "lblEMSCdesc"})};
		this.lblUSGSdescModel = {label : $L({value : "Focus on Americas and Pacific region", key : "lblUSGSdesc"})};
		this.lblRemModel = {label : $L({value : "Note: The more sources are selected, the longer the startup time of the app and the data loading will take and the possibility of multiple entries for the same earthquake will increase.", key : "lblRem"})};

		Ares.setupSceneAssistant(this);
		this.controller.setupWidget(Mojo.Menu.appMenu,
			this.attributes = {
				omitDefaultItems: true
			},
			this.model = {
				visible: true,
				items: [ 
					Mojo.Menu.editItem,
					{label: $L('Reset Preferences'), icon: "delete", command: "do-KillCookie"},
					// { label: $L("Help..."), command: 'do-myHelp' },
					{ label: $L("About SeismiX"), command: 'do-myAbout'}
				]
			}
		);
		this.$.hdrPrefs.setTitle($L("Preferences"));
		this.$.liseDist.setLabel($L("Display distance in"));
		this.liseDistModel.choices = [
		  {
		    label: $L("Kilometers (km)"),
		    value: "K"
		  },
		  {
		    label: $L("Miles (mi)"),
		    value: "M"
		  },
		  {
		    label: $L("Nautical Mile (NM)"),
		    value: "N"
		  }
		];
/*		this.$.liseInfo.setLabel($L("Display data on map"));
		this.liseInfoModel.choices = [
			{
			  label: $L("As Header"),
			  value: "H"
			},
			{
			  label: $L("As Bubble"),
			  value: "B"
			},
			{
			  label: $L("No data on map"),
			  value: "N"
			}
		];
*/
		this.$.collFilter.setTitleLabel($L("Filter Options"));
		this.$.collDisplay.setTitleLabel($L("Display Options"));
		this.$.collSources.setTitleLabel($L("Data Sources"));
		this.$.collOtherOpt.setTitleLabel($L("Other Options"));
		this.$.grpMyLoc.setLabel($L("My Location"));
		this.controller.modelChanged(this.liseDistModel);
//		this.controller.modelChanged(this.liseInfoModel);
	},

	handleCommand: function(event) {
		if (event.type === Mojo.Event.command) {
			if (event.command == "do-myAbout") {
				this.controller.stageController.pushScene("scnAbout");
			}
			if (event.command == 'do-KillCookie') { 
				try {
					var PrefsCookie = new Mojo.Model.Cookie('PrefsCookie');
					PrefsCookie.remove();
					this.$.txtLat.setValue(0);
					this.$.txtLon.setValue(0);
					this.$.sliMag.setValue(3);
					this.$.lblMagNb.setLabel("3+");
					this.$.sliRefresh.setValue(10);
					this.$.lblRefreshNb.setLabel("10 min.");
					this.$.liseDist.setValue("K");
//					this.$.liseInfo.setValue("H");
					this.$.cbPalmDark.setValue(0);
					this.$.cbTecPlates.setValue(0);
					this.$.sliMaxDist.setValue(20037);
					this.$.txtMaxDist.setValue(20037);
					this.$.cbOverride.setValue(1);
					this.$.cbEMSC.setValue(1);
					this.$.cbUSGS.setValue(1);
					Mojo.Controller.getAppController().showBanner({messageText: $L("Preferences resetted"), icon: "images/day-event-info-press.png"},{source: 'notification'}, "SeismiX");
				}
				catch(err) {
					Mojo.Controller.errorDialog($L("Could not reset preferences."));
				}
			}
			if (event.command == "do-myHelp") {
			}
		}
	},

	cleanup: function() {
/*		if ((this.cbEMSCModel.value == false) &&(this.cbUSGSModel.value == false)) {
			return false;
		}
*/		var PrefsCookie = new Mojo.Model.Cookie('PrefsCookie');
		try {
			PrefsCookie.put({
				myLat: this.txtLatModel.value,
				myLon: this.txtLonModel.value,
				myMag: this.sliMagModel.value,
				myPageRefresh: this.sliRefreshModel.value,
				myDist: this.liseDistModel.value,
				myMaxDist : this.txtMaxDistModel.value,
//				myInfo: this.liseInfoModel.value,
				myPalmDark: this.cbPalmDarkModel.value,
				myTecPlates: this.cbTecPlatesModel.value,
				myOverride: this.cbOverrideModel.value,
				myEMSC: this.cbEMSCModel.value,
				myUSGS: this.cbUSGSModel.value
			});
		}
		catch (err) {
			Mojo.Log.error(" ### Could not write preferences cookie.");
			Mojo.Controller.errorDialog($L("Saving the preferences failed."));
		}
		this.controller.stageController.parentSceneAssistant(this).needRefresh = true;
		this.controller.stageController.parentSceneAssistant(this).currPrefs.pageRefresh = this.sliRefreshModel.value;
		Ares.cleanupSceneAssistant(this);
	},
	

	sliMagChange: function(inSender, event) {
		this.lblMagNbModel.label = event.value + "+";
		this.controller.modelChanged(this.lblMagNbModel);
	},

	sliRefreshChange: function(inSender, event) {
		this.lblRefreshNbModel.label = event.value + " min.";
		this.controller.modelChanged(this.lblRefreshNbModel);
	},

	gps1Success: function(inSender, inResponse, inRequest) {
		if (inResponse !== 'undefined') {
			this.$.txtLat.setValue(inResponse.latitude);
			this.$.txtLon.setValue(inResponse.longitude);
			this.controller.get('btnGetMyLoc').mojo.deactivate();
		}	
	},

	gps1Failure: function(inSender, inError) {
		var errStr = $L("Could not get a location fix!") + " ";
		switch (inError.errorCode) {
			case  1: //Timeout
				errStr = errStr + $L("The GPS/LocationService timed out.");
				break;
			case 5: // LocationServiceOFF - No Location source available. Both Google and GPS are off.
				errStr = errStr + $L("No Location source available. Both Google and GPS are off.");
				break;
			case 6: // Permission Denied - The user has not accepted the terms of use for the Google Location Service, or the Google Service is off.
				errStr = errStr + $L("The user has not accepted the terms of use for the Google Location Service.");
				break;
			case 8: // The application has been temporarily blacklisted.
				errStr = errStr + $L({value : "The application has been temporarily blacklisted. Please switch 'Auto Locate' on in the Location Services app.", key : "Blacklisted"});
				break;
		}
		this.controller.showAlertDialog({
			preventCancel: false,
			onChoose: function(value) {
				this.controller.get('btnGetMyLoc').mojo.deactivate();
				if (value == 'LocSvc') {
					this.controller.serviceRequest('palm://com.palm.applicationManager', {
						method: 'launch',
						parameters:  {
							id: 'com.palm.app.location',
							params: {}
						}
					});
				}
			},
			title: $L("No GPS available"),
			message: errStr,
			choices:[
				{label:$L('Open Location Services'), value:"LocSvc", type:'affirmative'},
				{label:$L('Continue without GPS'), value:"GoOn", type: 'dismiss'}
			]
		});
	},

	btnGetMyLocTap: function(inSender, event) {
		this.controller.get('btnGetMyLoc').mojo.activate();
		this.$.gps1.getCurrentPosition();
	},
	sliMaxDistChange: function(inSender, event) {
		this.txtMaxDistModel.value = event.value;
		this.controller.modelChanged(this.txtMaxDistModel);
	}
};